﻿using RantopadService.Business;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RantopadService.Info
{
    public partial class OptionInfo : System.Web.UI.Page
    {
        private string id = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            id = Request.QueryString["id"];
            initData();
        }

        private void initData()
        {
            var row= OptionBusiness.getOptionByID(id);
            OPTIONID.Text = row["ID"].ToString();
            DEVICE_NAME.Text = row["DEVICE_NAME"].ToString();
            NAME.Text = row["OPTION_NAME"].ToString();
            USERID.Text = row["USER_ID"].ToString();
            REFRESE.Text = row["REFRESE"].ToString();
            SENSIBILITY.Text = row["SENSIBILITY"].ToString();
            UP.Text = row["UP"].ToString();
            DOWN.Text = row["DOWN"].ToString();
            //加载按键详细设置信息
            //var table=KeySettingBusiness.getAllKeySettingsByID(row["ID"].ToString());
            GridView1.DataSource = KeySettingBusiness.getAllKeySettingsNameByID(row["ID"].ToString());
            GridView1.DataBind();
            //加载评论
            GridView2.DataSource = CommentBusiness.getAllCommentsByOptionID(row["ID"].ToString());
            GridView2.DataBind();
        }
        protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = GridView2.DataKeys[e.RowIndex].Value.ToString();

            if (CommentBusiness.delCommentByID(id) > 0)
                Response.Write("<script>alert('删除成功！')</script>");
            else
                Response.Write("<script>alert('删除失败！')</script>");
            initData();
        }

        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    ((LinkButton)e.Row.Cells[4].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除：\"" + e.Row.Cells[0].Text + "\"吗?')");
                }
            } 
        }
        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {

        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            initData();
        }
        //        Response.Redirect("../Info/OptionInfo.aspx?id=" + selectedRow.Cells[0].Text);
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "MoreInfo")
            {
                // Convert the row index stored in the CommandArgument
                // property to an Integer.
                int index = Convert.ToInt32(e.CommandArgument);

                // Get the last name of the selected author from the appropriate
                // cell in the GridView control.
                GridViewRow selectedRow = GridView1.Rows[index];
                //TableCell contactName = selectedRow.Cells[1];
                //string contact = contactName.Text;
                //Msg.Text = contact;
                if (selectedRow.Cells[2].Text=="宏")
                Response.Redirect("../Info/MacroInfo.aspx?userName=" + USERID.Text + "&macroName=" + selectedRow.Cells[3].Text);

            }
        } 
    }
}